package com.ponhu.ea2.stockcenter.provider.service.entity.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ponhu.ea2.stockcenter.enums.DefinedSystem;
import com.ponhu.ea2.stockcenter.provider.constants.CommonConstant;
import com.ponhu.ea2.stockcenter.provider.entity.StockInventoryReal;
import com.ponhu.ea2.stockcenter.provider.entity.StockInventoryUnreal;
import com.ponhu.ea2.stockcenter.provider.mapper.StockInventoryUnrealMapper;
import com.ponhu.ea2.stockcenter.provider.service.biz.stocksales.SalesStockChangeCoreHandler;
import com.ponhu.ea2.stockcenter.provider.service.entity.StockInventoryUnrealService;
import com.ponhu.ea2.stockcenter.provider.support.vo.InventoryInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import static com.ponhu.ea2.stockcenter.provider.support.enums.StockChangeInnerEventEnum.ADD_VIRTUAL_SALES_STOCK;
import static com.ponhu.ea2.stockcenter.provider.support.enums.StockChangeInnerEventEnum.DELETE_VIRTUAL_SALES_STOCK;

/**
 *
 */
@Service
public class StockInventoryUnrealServiceImpl extends ServiceImpl<StockInventoryUnrealMapper, StockInventoryUnreal>
        implements StockInventoryUnrealService {

    @Autowired
    private SalesStockChangeCoreHandler salesStockChangeCoreHandler;

    @Override
    public boolean isUnrealInventory(String inventoryCode) {

        List<StockInventoryUnreal> inventoryUnrealList = this.list(Wrappers.lambdaQuery(StockInventoryUnreal.class)
                .eq(StockInventoryUnreal::getInventoryCode, inventoryCode)
        );

        return CollectionUtils.isNotEmpty(inventoryUnrealList);
    }

    @Override
    public List<StockInventoryUnreal> getUnRealInventoryStockList(Collection<String> inventoryCodeList) {
        return this.list(
                Wrappers.lambdaQuery(StockInventoryUnreal.class)
                        .eq(StockInventoryUnreal::getStatus, CommonConstant.INIT_STATUS.FINISHED)
                        .in(StockInventoryUnreal::getInventoryCode, inventoryCodeList));
    }

    @Override
    public boolean createAndInitStockData(Long goodsId, Long inventoryId, String inventoryCode, int stockNumber) {
        inventoryCode = inventoryCode.trim();
        //判断是否存在，如果已经存在那么直接返回成功
        StockInventoryUnreal inventory = this.getOne(Wrappers.lambdaQuery(StockInventoryUnreal.class)
                .eq(StockInventoryUnreal::getGoodsId, goodsId)
                .eq(StockInventoryUnreal::getInventoryId, inventoryId)
                .eq(StockInventoryUnreal::getInventoryCode, inventoryCode));
        if (inventory != null) {
            return true;
        }

        //不存在那么插入新数据
        StockInventoryUnreal stockInventoryUnreal = new StockInventoryUnreal();
        stockInventoryUnreal.setInventoryId(inventoryId)
                .setGoodsId(goodsId)
                .setInventoryCode(inventoryCode)
                .setSaleStockNum(stockNumber)
                .setStatus(CommonConstant.INIT_STATUS.FINISHED);

        this.save(stockInventoryUnreal);


        //记录日志
        InventoryInfoVO inventoryInfoVO = new InventoryInfoVO()
                .setInventoryCode(stockInventoryUnreal.getInventoryCode())
                .setInventoryQuantity(-1);

        salesStockChangeCoreHandler.saveSalesLevelStockChangeLog(null, DefinedSystem.GOODS_CENTER.ordinal(),
                ADD_VIRTUAL_SALES_STOCK.getCode(), Collections.singletonList(inventoryInfoVO),
                new ArrayList<>(), Collections.singletonList(stockInventoryUnreal));

        return true;
    }
}




